﻿using Kang.Monitor.Wpf.ViewModels;
using Microsoft.Xaml.Behaviors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Kang.Monitor.Wpf.Behaviors
{
    public class PasswordBoxBehavior : Behavior<UIElement>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.LostKeyboardFocus += AssociatedObjectLostKeyboardFocus;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.LostKeyboardFocus -= AssociatedObjectLostKeyboardFocus;
            base.OnDetaching();
        }

        private void AssociatedObjectLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var associatedPasswordBox = AssociatedObject as PasswordBox;
            var newPasswordBox = sender as PasswordBox;
            if (associatedPasswordBox != null)
            {
                // Set your view-model's Password property here
                (newPasswordBox.DataContext as LoginViewModel).Pwd = associatedPasswordBox.Password;
            }
        }
    }
}